﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using eMeng;
using ValidityCheck;
using JavaScript;
using SQL;
using MessageOutPut;
public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        InitLinkTable();
        InitMenu();
        InitSearchCatalog();
    }

    protected void InitSearchCatalog()
    {
        CatalogDropDownList.Items.Clear();
        ListItem noneListItem = new ListItem("无限制", "0");
        CatalogDropDownList.Items.Add(noneListItem);
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var Catalogs = from catalog in dc.Catalogs
                       where catalog.Catalog_id!=9
                         select new
                         {
                             Catalog_id = catalog.Catalog_id,
                             CatalogName = catalog.CatalogName
                         };

        foreach (var catalog in Catalogs)
        {
            ListItem theListItem = new ListItem(catalog.CatalogName, catalog.Catalog_id.ToString());
            CatalogDropDownList.Items.Add(theListItem);
        }
    }

    protected void InitLinkTable()
    {
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var LinksInfos = from linksinfo in dc.LinksInfos
                         select new
                         {
                             标题 = linksinfo.Title,
                             内容 = linksinfo.Content,
                             发布 = linksinfo.isPublished
                         };
        TableRow tr = new TableRow();
        tr.HorizontalAlign = HorizontalAlign.Center;
        tr.VerticalAlign = VerticalAlign.Middle;
        TableCell tcTitle = new TableCell();
        tcTitle.HorizontalAlign = HorizontalAlign.Center;
        tcTitle.VerticalAlign = VerticalAlign.Middle;
        HyperLink Lbtitle = new HyperLink();
        Lbtitle.Text = "相关链接：";
        tcTitle.Controls.Add(Lbtitle);
        tcTitle.CssClass = "BottomHyperLink";
        tr.Cells.Add(tcTitle);
        foreach (var linksinfo in LinksInfos)
        {
            //          TableCell tcboard = new TableCell();
            //         Label Lbboard = new Label();
            HyperLink link;
            TableCell tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.VerticalAlign = VerticalAlign.Middle;
            link = new HyperLink();
            link.Text = linksinfo.标题;
            link.NavigateUrl = linksinfo.内容;
            link.CssClass = "BottomHyperLink";
            tc.Controls.Add(link);
            tr.Cells.Add(tc);
        }
        Linktable.Rows.Add(tr);
    }

    protected void InitMenu()
    {
        MainMenu.Items.Clear();
        MenuItem MainPage = new MenuItem();
        MainPage.Enabled = true;
        MainPage.Selectable = true;
        MainPage.Selected = false;
        MainPage.Value = MainPage.ToolTip = MainPage.Text = "首页";
        MainPage.NavigateUrl = "Default.aspx";
        MainMenu.Items.Add(MainPage);
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var Catalogs = from catalog in dc.Catalogs
                       select new
                       {
                           Catalog_id = catalog.Catalog_id,
                           Name = catalog.CatalogName
                       };
        foreach (var catalog in Catalogs)
        {
            if (catalog.Catalog_id != 9)
            {
                MenuItem menuitem = new MenuItem();
                menuitem.Enabled = true;
                menuitem.Selectable = true;
                menuitem.Selected = false;
                menuitem.ToolTip = menuitem.Text = catalog.Name;
                menuitem.Value = catalog.Catalog_id.ToString();
                if (catalog.Catalog_id==1)
                {
                    menuitem.NavigateUrl = "PictureNewsList.aspx";
                }
                else if (catalog.Catalog_id == 2)
                {
                    menuitem.NavigateUrl = "CollegeNewsList.aspx";
                }
                else if (catalog.Catalog_id == 3)
                {
                    menuitem.NavigateUrl = "NoticeList.aspx";
                }
                else if (catalog.Catalog_id == 4)
                {
                    menuitem.NavigateUrl = "IndependentInstituteDynamicList.aspx";
                }
                else if (catalog.Catalog_id == 5)
                {
                    menuitem.NavigateUrl = "CampusDynamicList.aspx";
                } 
                MainMenu.Items.Add(menuitem);
            }
        }
        dc.Dispose();
    }
    protected void Login_Button_Click(object sender, EventArgs e)
    {
        if (Check.IsNull(UserName.Value))
        {
            Response.Write("<script language=\"JavaScript\">alert(\"用户名为空！\");history.go(-1);</script>");
        }
        else if (Check.IsNull(Password.Value))
        {
            Response.Write("<script language=\"JavaScript\">alert(\"密码为空！\");history.go(-1);</script>");
        }
        else
        {
            SymmCrypto sc = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);

            string password = sc.Encrypting(Password.Value, "binhai");
            UserInfo userInfo = new UserInfo();
            userInfo.UserName = UserName.Value;
            userInfo.Password = password;
            if (SQLHelper.IsExist(userInfo))
            {
                if (SQLHelper.getGroupID(UserName.Value) == 3)
                {
                    Response.Write("<script language=\"JavaScript\">alert(\"用户名密码不正确！\");history.go(-1);</script>");
                }
                else
                {
                    Session.Add("admin", "true");
                    Session.Add("GroupID", SQLHelper.getGroupID(UserName.Value));
                    Session.Add("UserID", SQLHelper.getUserID(UserName.Value));
                    Session.Add("InitType", "1");
                    Response.Redirect("Admin/AdminHome.aspx");
                }
            }
            else
            {
                Response.Write("<script language=\"JavaScript\">alert(\"用户名密码不正确！\");history.go(-1);</script>");
            }
        }
    }
    protected void SearchButton_Click(object sender, EventArgs e)
    {
        if (Check.IsNull(KeyWord.Value))
        {

        }
        else
        {
            Session.Add("Search", "true");
            Session.Add("SearchCatalog", CatalogDropDownList.SelectedValue);
            Session.Add("SearchKeyWord", KeyWord.Value);
            Response.Redirect("SearchList.aspx");
        }
    }
}
